package com.design.movieticket.service.Impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.design.movieticket.entity.Balance;
import com.design.movieticket.entity.Recharge;
import com.design.movieticket.mapper.BalanceMapper;
import com.design.movieticket.mapper.RechargeMapper;
import com.design.movieticket.service.BalanceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

@Service
public class BalanceServiceImpl extends ServiceImpl<BalanceMapper, Balance> implements BalanceService {
    @Resource
    private BalanceMapper balanceMapper;

    @Resource
    private RechargeMapper rechargeMapper;

    @Override
    public Balance getBalanceByUserId(Long userId) {
        return super.getOne(new LambdaQueryWrapper<Balance>().eq(Balance::getUserId,userId));
    }

    @Override
    public String wxRecharge(String username, Long userId, BigDecimal money) {
        String result = "充值成功";
        if (username == null && userId == null && money == null){
            return "充值失败";
        }
        Recharge recharge = new Recharge();
        recharge.setUserId(userId);
        recharge.setUsername(username);
        recharge.setRechargeMoney(money);
        rechargeMapper.insert(recharge);

        Balance balance = balanceMapper.selectOne(new LambdaQueryWrapper<Balance>().eq(Balance::getUserId,userId));
        balance.setBalance(balance.getBalance().add(money));
        balanceMapper.updateById(balance);
        return result;
    }
}
